package com.kac.service.Impl;

import com.codingapi.txlcn.client.bean.DTXLocal;
import com.codingapi.txlcn.commons.annotation.DTXPropagation;
import com.codingapi.txlcn.commons.annotation.LcnTransaction;
import com.codingapi.txlcn.commons.annotation.TccTransaction;
import com.codingapi.txlcn.commons.annotation.TxcTransaction;
import com.kac.entity.KacProduct;
import com.kac.repository.KacProductRepository;
import com.kac.service.KacProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @author Qilin
 * @date  2019-01-16
 */
@Service
@Slf4j
public class KacProductServiceImpl implements KacProductService {

    @Autowired
    private KacProductRepository kacProductRepository;
    private ConcurrentHashMap<String, Long> ids = new ConcurrentHashMap<>();


    @Override
//    @TccTransaction(dtxp = DTXPropagation.SUPPORTS)
    @TxcTransaction(dtxp = DTXPropagation.SUPPORTS)
    @Transactional
    public void save()  {
            KacProduct product = new KacProduct();
            product.setName("product");
//            kacProductRepository.save(product);
//            ids.put(DTXLocal.cur().getGroupId(), Long.valueOf(product.getId()));

        int rs1 =  kacProductRepository.save(product).getId();

        int v = 100/0;
    }

    @Override
    public String test() {
        return "asdasdasdass";
    }

//    public void confirmSave() {
//        log.info("tcc-confirm-" + DTXLocal.getOrNew().getGroupId());
//        ids.remove(DTXLocal.getOrNew().getGroupId());
//    }
//
//    public void cancelSave() {
//        if(null != ids.get(DTXLocal.getOrNew().getGroupId())){
//            kacProductRepository.deleteById((ids.get(DTXLocal.getOrNew().getGroupId())).intValue());
//        }
//        System.out.println("wqe");
//        System.out.println(ids.get(DTXLocal.getOrNew().getGroupId()));
//        log.info("tcc-cancel-" + DTXLocal.getOrNew().getGroupId());
//        log.info("rollback-product");
//
//    }
}
